package com.javabi.sizeof.definition.quick;

import java.util.LinkedList;
import java.util.Set;

import com.javabi.sizeof.ClassDefinition;
import com.javabi.sizeof.ClassDefinitionMap;
import com.javabi.sizeof.MemoryUtil;

/**
 * A LinkedList Quick Definition.
 * <p>
 * This definition for {@link LinkedList}s is over twice as fast as the default
 * definition, however it does NOT record the Entry instances backing its
 * elements. Although this is an issue, it is not particularly unsafe, as very
 * rarely will anyone reference the Entry instances externally.
 */
@SuppressWarnings("unchecked")
public class LinkedListQuickDefinition extends ClassDefinition<LinkedList> {

	@Override
	public long sizeOf(LinkedList list) {
		return 44 + ((long) list.size() * 24);
	}

	@Override
	public final boolean hasElements(LinkedList list) {
		return !list.isEmpty();
	}

	@Override
	public long sizeOfElements(LinkedList list, ClassDefinitionMap definitionMap, Set<Object> instanceSet, long size) {
		for (Object element : list) {
			if (element != null) {
				size += MemoryUtil.sizeOf(element, definitionMap, instanceSet);
			}
		}
		return size;
	}

}
